package com.hnichr.ump.plan.controller;

import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.entity.Dept;
import com.mxpioframework.security.service.DeptService;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/plan/dept/v1/")
@Tag(name = "PlanDeptController", description = "部门计划部门信息接口")
public class PlanDeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("byuser")
    @Operation(summary = "获取当前登录用户所属部门", description = "获取当前登录用户所属部门", method = "GET")
    public Result<Dept> getDeptByUsername() {
        Set<String> deptIds = deptService.getDeptKeysByUser(SecurityUtils.getLoginUsername(),"id");
        if(CollectionUtils.isEmpty(deptIds)){
            return Result.error("用户不属于任何部门");
        }
        Criteria criteria = Criteria.create().addCriterion("id", Operator.EQ,deptIds.iterator().next());
        List<Dept> deptList =deptService.getDeptTree(criteria);
        if(CollectionUtils.isEmpty(deptList)){
            return Result.error("部门不存在");
        }
        return Result.OK(deptList.get(0));
    }
}
